package com.fmdj.mis.api.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.db.Page;
import com.fmdj.common.util.CommonResult;
import com.fmdj.common.util.CosUtil;
import com.fmdj.common.util.PageUtils;
import com.fmdj.mis.api.controller.form.SelectDriverAuthInfoForm;
import com.fmdj.mis.api.controller.form.SelectDriverByPageForm;
import com.fmdj.mis.api.controller.form.UpdateDriverRealAuthForm;
import com.fmdj.mis.api.feign.DrServiceApi;
import com.fmdj.mis.api.service.DriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class DriverServiceImpl implements DriverService {

    @Autowired
    private DrServiceApi drServiceApi;

    @Autowired
    private CosUtil cosUtil;
    @Override
    public PageUtils selectDriverByPage(SelectDriverByPageForm form) {
        CommonResult commonResult = drServiceApi.selectDriverPage(form);
        Object page =  commonResult.get(CommonResult.RETURN_RESULT);

        // 将page对象转换为PageUtils对象 这里不能用 PageUtils pageUtils = (PageUtils)commonResult.get(CommonResult.RETURN_RESULT) 因为pageUtils不是Object的子类型 无法向下转型 ;
        PageUtils pageUtils = BeanUtil.toBean(page, PageUtils.class);
        return pageUtils;
    }

    @Override
    public HashMap selectDriverAuthInfo(byte realAuth, Long driverId) {

        HashMap result = new HashMap<>();
        /**
         * realAuth
         *  1未认证（认证失败），2已认证，3审核中
         *  后台管理员在认证审批的时候拒绝该司机的realAuth就会=1，需要司机重新提交资料才会更新为3
         * */
        if (realAuth == 2 || realAuth == 3) {


            SelectDriverAuthInfoForm form = new SelectDriverAuthInfoForm();
            form.setDriverId(driverId);
            CommonResult commonResult = drServiceApi.selectDriverAuthInfo(form);
            HashMap driverAuthInfo = (HashMap) commonResult.get(CommonResult.DRIVER_AUTH_INFO);
            /*
             * 先取出6张图片
             * */
            String idcardFront = MapUtil.getStr(driverAuthInfo, "idcardFront");
            String idcardBack = MapUtil.getStr(driverAuthInfo, "idcardBack");
            String idcardHolding = MapUtil.getStr(driverAuthInfo, "idcardHolding");
            String drcardFront = MapUtil.getStr(driverAuthInfo, "drcardFront");
            String drcardBack = MapUtil.getStr(driverAuthInfo, "drcardBack");
            String drcardHolding = MapUtil.getStr(driverAuthInfo, "drcardHolding");
            /*
             * 将上面的图片生成临时的外网访问URL
             * */
            idcardFront = idcardFront.length() > 0 ? cosUtil.getPrivateFileUrl(idcardFront) : "";
            idcardBack = idcardBack.length() > 0 ? cosUtil.getPrivateFileUrl(idcardBack) : "";
            idcardHolding = idcardHolding.length() > 0 ? cosUtil.getPrivateFileUrl(idcardHolding) : "";
            drcardFront = drcardFront.length() > 0 ? cosUtil.getPrivateFileUrl(drcardFront) : "";
            drcardBack = drcardBack.length() > 0 ? cosUtil.getPrivateFileUrl(drcardBack) : "";
            drcardHolding = drcardHolding.length() > 0 ? cosUtil.getPrivateFileUrl(drcardHolding) : "";
            /*
             * 保存到结果集中
             * */
            driverAuthInfo.replace("idcardFront", idcardFront);
            driverAuthInfo.replace("idcardBack", idcardBack);
            driverAuthInfo.replace("idcardHolding", idcardHolding);
            driverAuthInfo.replace("drcardFront", drcardFront);
            driverAuthInfo.replace("drcardBack", drcardBack);
            driverAuthInfo.replace("drcardHolding", drcardHolding);
            //返回结果集
            result.put(CommonResult.DRIVER_AUTH_INFO, driverAuthInfo);
        }
            //返回结果集
            return result;
    }

    @Override
    public int updateDriverRealAuth(UpdateDriverRealAuthForm form) {
        CommonResult commonResult = drServiceApi.updateDriverRealAuth(form);
        int rows = (int) commonResult.get(CommonResult.RETURN_ROW);
        return rows;
    }
}
